<?php

class ManufacturerController extends MainController implements ControllerInterface {
	
	
	public function IndexAction($param)
	{
		
		$table_data = $this->getTableData(
				'manufacturer_table', 
				$param, 
				array(
					'o' => 'name',
					'v' => 'malejaco',
					't' => 10
				)
			);
		$v = $table_data['v'] == 'malejaco'?'DESC':'ASC';
		$oTable = array(
			'name' => 'manufacturer.name',
			'id' => 'manufacturer.id_manufacturer',
		);	
		$o = $oTable[$table_data['o']];
		$p = isset($param['strona'])?$param['strona']:1;
		
		$dalData = ManufacturerDAL::GetDalDataObj(
				array(
					'count' => true,
				)	
			);
		$this->smarty->assign('c', 	ManufacturerDAL::getResult($dalData));
		
		$dalData->setCount(false);
		$dalData->setLimit((($p-1)*$table_data['t']).', '.$table_data['t']);
		
		$dalData->setOrderBy($o . ' ' . $v);
		$dalData->setSelectFields(array(
			'productsCount' => ' (SELECT COUNT(id_product) FROM product WHERE id_manufacturer = manufacturer.id_manufacturer)'
		));
		
		$arrayObjManufacturers = ManufacturerDAL::GetResult($dalData);

		
		$this->smarty->assign('arrayObjManufacturers', $arrayObjManufacturers);		
		
	}
	
	public function AddAction($param)
	{
		if(Request::IsPost())
		{
			$man = new Manufacturer();
			$man->setName(Request::Get('name'));
			$man->setActive(Request::Get('active'));
			
			$man->setSlug(ManufacturerDAL::GenerateUniqueSlug($man->getName()));
			
			if(isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK)
			{
				$img = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
				if($img)
				{
					if(!is_dir(PATH_STATIC_CONTENT . 'upload/manufacturer'))
						mkdir(PATH_STATIC_CONTENT . 'upload/manufacturer',0777,true);
					
					$fileName = preg_replace('~[^\\pL\d\.]+~u', '-', $_FILES['image']['name']);
					
					$temp = $fileName;
					$counter = 0;
					while(is_file(PATH_STATIC_CONTENT . 'upload/manufacturer/'.$temp))
						$temp = ++$counter.'_'.$fileName;
					
					$fileName = $temp;
					
					move_uploaded_file($_FILES['image']['tmp_name'], PATH_STATIC_CONTENT . 'upload/manufacturer/' . $fileName);
					$man->setImage('upload/manufacturer/' . $fileName);
				}	
			}
			
			ManufacturerDAL::Save($man->GetDalDataObj());
			$this->addRedirect(Router::GenerateUrl('manufacturerIndex',array('Manufacturer' => 'Index')),0);
			
			
		}
	}
	
	public function EditAction($param)
	{
		if($param['id'])
		{
			$man = array_pop(ManufacturerDAL::GetResult(ManufacturerDAL::GetDalDataObj(array('limit'=>1, 'condition'=>array('id_manufacturer'=>array($param['id']))))));
		}
		
		if(empty ($man) || !isset($param['id']))
		{
			$this->addRedirect(Router::GenerateUrl('manufacturerIndex',array('Manufacturer' => 'Index')),0);
		}
		
		if(Request::IsPost())
		{
			$man->setName(Request::Get('name'));
			$man->setActive(Request::Get('active'));
			
			if(isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK)
			{
				$img = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
				if($img)
				{
					if(!is_dir(PATH_STATIC_CONTENT . 'upload/manufacturer'))
						mkdir(PATH_STATIC_CONTENT . 'upload/manufacturer',0777,true);
					
					$fileName = preg_replace('~[^\\pL\d\.]+~u', '-', $_FILES['image']['name']);
					
					$temp = $fileName;
					$counter = 0;
					while(is_file(PATH_STATIC_CONTENT . 'upload/manufacturer/'.$temp))
						$temp = ++$counter.'_'.$fileName;
					
					$fileName = $temp;
					
					move_uploaded_file($_FILES['image']['tmp_name'], PATH_STATIC_CONTENT . 'upload/manufacturer/' . $fileName);
					$man->setImage('upload/manufacturer/' . $fileName);
				}
			}
			
			if(Request::Get('slug') != $man->getSlug())	$man->setSlug(ManufacturerDAL::GenerateUniqueSlug(Request::Get('slug')));
			
			ManufacturerDAL::Save($man->GetDalDataObj());	
			$this->addRedirect(Router::GenerateUrl('manufacturerIndex',array('Manufacturer' => 'Index')),0);
		}
		
		/* categories */
		$catList = CategoryDAL::GetResult(
				CategoryDAL::GetDalDataObj(
					array(
						'condition' => array(
							'parent' => array(array('condition' => '<', 'value' => '1'))
						),
						'join' => array(
							'CategoryDescription' => ' LEFT JOIN category_description ON (category_description.id_category = category.id_category AND category_description.id_language = ' . Registry::Get('APPLICATION_LANGUAGE_ID') . ')'
						)
					)
				)
			);
		/* end categories*/
		
		$this->smarty->assign('catList', $catList);
		
		$this->smarty->assign('obj', $man);
	}
	
	public function DeleteAction($param)
	{
		$this->setNoRender();
		if($param['id'])
		{
			$man = array_pop(ManufacturerDAL::GetResult(ManufacturerDAL::GetDalDataObj(array('limit'=>1, 'condition'=>array('id_manufacturer'=>array($param['id']))))));
		}
		
		if(empty ($man) || !isset($param['id']))
		{
			$this->addRedirect(Router::GenerateUrl('manufacturerIndex',array('Manufacturer' => 'Index')),0);
		}
		
		ManufacturerDAL::Delete($man->getDalDAtaObj());
		$this->addRedirect(Router::GenerateUrl('manufacturerIndex',array('Manufacturer' => 'Index')),0);
	}
	
	public function preDispatch($param)
	{
		$this->Run($param);
		$this->RunShared('Auth', array());		
	}
	
	public function postDispatch($param)
	{
		
	}
	
	
}
